package com.learning.spring.ioc.type.conversion.converter;

import com.google.common.base.Splitter;
import com.learning.spring.ioc.type.conversion.entity.Address;
import org.springframework.core.convert.converter.Converter;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * ClassName: AddressConverter
 * Description:  Address 类型转换器组件扩展
 * Date: 2020/7/22 9:52 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class AddressConverter implements Converter<String, Address> {
    private final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Address convert(String source) {

        // 将格式为 <id>:<name> 的字符串转换为 Address 对象
        List<String> list = Splitter.on(":").splitToList(source);
        Address address = new Address();
        address.setId(Integer.parseInt(list.get(0)));
        address.setName(list.get(1));
        return address;
    }
}